from . import admin
from app import db
from app.schemas import ArticleOutSchema
from app.models import Article, Category, User
from flask import render_template, request, jsonify
from app.some_func import wapper, fail_api, success_api, table_api
import html
import os
from werkzeug.utils import secure_filename
# 文章管理
@admin.get('/article/')
@wapper
def article_main():
    return render_template('admin/article/main.html')
# 表格数据
@admin.get('/article/data')
@wapper
def article_table():
    title = request.args.get('title')
    filters = []
    if title:
        filters.append(Article.title.contains(title))
    articles = Article.query.filter(*filters).order_by(Article.id.desc()).paginate(page=request.args.get('page', type=int),per_page=request.args.get('limit', type=int),error_out=False)
    return table_api(data=ArticleOutSchema(many=True).dump(articles), count=articles.total)
# 文章增加
@admin.get('/article/add')
@wapper
def article_add():
    category_list = Category.query.all()
    author_list = User.query.all()
    return render_template('admin/article/add.html', category_list=category_list, author_list=author_list)
# 文章保存
@admin.post('/article/save')
@wapper
def article_save():
    if not request.json.get('title') or not request.json.get('author') or not request.json.get('content') or not request.json.get('category_id'):
        return fail_api(msg="所有内容不得为空")
    title = request.json.get('title')
    author = request.json.get('author')
    content = html.unescape(request.json.get('content'))
    category_id = request.json.get('category_id')
    article = Article(title=title, author=author, content=content, category_id=category_id)
    db.session.add(article)
    db.session.commit()
    return success_api(msg="增加成功")
# 编辑文章
@admin.get('/article/edit/<int:id>')
@wapper
def article_edit(id):
    article = Article.query.filter(Article.id == id).first()
    category_list = Category.query.all()
    author_list = User.query.all()
    return render_template('admin/article/edit.html', category_list=category_list, author_list=author_list, article=article)
# 删除文章
@admin.delete('/article/remove/<int:id>')
@wapper
def article_delete(id):
    res = Article.query.filter_by(id=id).delete()
    db.session.commit()
    if not res:
        return fail_api(msg="文章删除失败")
    return success_api(msg="文章删除成功")
# 更新文章
@admin.put('/article/update')
@wapper
def article_update():
    if not request.json.get('title') or not request.json.get('author') or not request.json.get('content') or not request.json.get('category_id'):
        return fail_api(msg="所有内容不得为空")
    id = request.json.get('id')
    title = request.json.get('title')
    author = request.json.get('author')
    content = html.unescape(request.json.get('content'))
    category_id = request.json.get('category_id')
    Article.query.filter_by(id=id).update({'title':title, 'author':author, 'content':content, 'category_id':category_id})
    db.session.commit()
    return success_api(msg="更新成功")
# 上传文件
@admin.post('/article/upload')
@wapper
def article_upload():
    if 'file' in request.files:
        file = request.files['file']
        upload_dir = os.path.join('app/static/upload', secure_filename(file.filename))
        file.save(upload_dir)
        data = {
            "msg": "上传成功",
            "code": 0,
            "success": True,
            "location": '/'+os.path.join('static/upload',secure_filename(file.filename))
        }
        return jsonify(data)
    return fail_api(msg="出错啦")